package joey.project1

import groovy.swing.SwingBuilder

import java.awt.Color
import java.util.logging.FileHandler
import java.util.logging.Level
import java.util.logging.Logger

import javax.swing.BorderFactory
import javax.swing.JButton
import javax.swing.JFrame
import javax.swing.JLabel
import javax.swing.JOptionPane
import javax.swing.JWindow
import javax.swing.border.Border

import joey.project1.model.Model1
import joey.project1.view.ImagePanel
import joey.project1.view.SizeImageIconFactory
import joey.project1.view.ViewUtil

class Run {
	public static void main(args){
		try{
			run()
		}catch(Throwable e){
			Logger logger = Logger.getLogger("Joey Project1")
			FileHandler fileHandler = new FileHandler("./log/myLogger.log")
			logger.addHandler(fileHandler)

			logger.log(Level.SEVERE, "error",e)
		}
	}

	private static void run(){
		Model1 model1 = Model1.instance

		SwingBuilder swing = new SwingBuilder()
		// 註冊自訂元件
		swing.registerFactory("imageIcon", new SizeImageIconFactory())
		swing.registerBeanFactory("imagePanel", ImagePanel)

		// 邊框物件
		Border blackBorder = BorderFactory.createMatteBorder(model1.imgBorderWidth, model1.imgBorderWidth, model1.imgBorderWidth, model1.imgBorderWidth, Color.white)
		Border redBorder = BorderFactory.createMatteBorder(model1.imgBorderWidth, model1.imgBorderWidth, model1.imgBorderWidth, model1.imgBorderWidth, Color.red)

		// 取照片方式
		def _img = { String imgFile, String show, int width, int height ->
			if(model1.isShowFileName()){
				// 有顯示檔案名
				swing.label(
						icon:swing.imageIcon(file:imgFile, width:width,height:height),
						text:show,
						horizontalTextPosition:JLabel.CENTER,
						verticalTextPosition:JLabel.BOTTOM,
						border:blackBorder)
			}else{
				// 無顯示檔案名
				swing.label(
						icon:swing.imageIcon(file:imgFile, width:width,height:height),
						border:blackBorder)
			}
		}

		// 中央照片
		String centerImage = model1.centerImage
		int width = model1.imgWidth
		int height = model1.imgHeight

		List<File> lotteryFiles
		List<JLabel> labels

		JFrame rootFrame
		JWindow winnerWindow
		JLabel winnerWindowLabel
		// 畫swing
		swing.edt {
			rootFrame = frame(title:'Frame', defaultCloseOperation:JFrame.EXIT_ON_CLOSE, pack:true, show:true){
				imagePanel(model1.backgroundImage()){
					vbox{
						// 上方照片
						imagePanel(model1.backgroundImage1()){
							label1 = _img(centerImage,' ',width,height)
							label2 = _img(centerImage,' ',width,height)
							label3 = _img(centerImage,' ',width,height)
						}
						// 中間照片
						imagePanel(model1.backgroundImage2()){
							label4 = _img(centerImage,' ',width,height)
							label5 = _img(centerImage,' ',width,height)
							label6 = _img(centerImage,' ',width,height)
						}
						// 下方照片
						imagePanel(model1.backgroundImage3()){
							label7 = _img(centerImage,' ',width,height)
							label8 = _img(centerImage,' ',width,height)
							label9 = _img(centerImage,' ',width,height)
						}
						// 按鈕區
						imagePanel(model1.backgroundImage4()){
							button1 = button(label:'選人',actionPerformed:{
								// 按選人
								Thread.start {
									lotteryFiles = model1.randomLotteryFiles()

									ViewUtil.invokeLater {
										label1.icon = swing.imageIcon(file:centerImage, width:width,height:height)
										label2.icon = swing.imageIcon(file:centerImage, width:width,height:height)
										label3.icon = swing.imageIcon(file:centerImage, width:width,height:height)
										label4.icon = swing.imageIcon(file:centerImage, width:width,height:height)
										label6.icon = swing.imageIcon(file:centerImage, width:width,height:height)
										label7.icon = swing.imageIcon(file:centerImage, width:width,height:height)
										label8.icon = swing.imageIcon(file:centerImage, width:width,height:height)
										label9.icon = swing.imageIcon(file:centerImage, width:width,height:height)

										label1.border = blackBorder
										label2.border = blackBorder
										label3.border = blackBorder
										label4.border = blackBorder
										label5.border = redBorder
										label6.border = blackBorder
										label7.border = blackBorder
										label8.border = blackBorder
										label9.border = blackBorder
									}
									model1.play_wav_drum_start.play_loop()
									Thread.sleep(model1.time_select)
									model1.play_wav_drum_winner.play()
									ViewUtil.invokeLater {
										label1.icon = swing.imageIcon(file:lotteryFiles[0], width:width,height:height)
										label1.text = model1.filterName(lotteryFiles[0])
									}
									Thread.sleep(model1.time_select)
									model1.play_wav_drum_winner.play()
									ViewUtil.invokeLater {
										label2.icon = swing.imageIcon(file:lotteryFiles[1], width:width,height:height)
										label2.text = model1.filterName(lotteryFiles[1])
									}
									Thread.sleep(model1.time_select)
									model1.play_wav_drum_winner.play()
									ViewUtil.invokeLater {
										label3.icon = swing.imageIcon(file:lotteryFiles[2], width:width,height:height)
										label3.text = model1.filterName(lotteryFiles[2])
									}
									Thread.sleep(model1.time_select)
									model1.play_wav_drum_winner.play()
									ViewUtil.invokeLater {
										label6.icon = swing.imageIcon(file:lotteryFiles[4], width:width,height:height)
										label6.text = model1.filterName(lotteryFiles[4])
									}
									Thread.sleep(model1.time_select)
									model1.play_wav_drum_winner.play()
									ViewUtil.invokeLater {
										label9.icon = swing.imageIcon(file:lotteryFiles[7], width:width,height:height)
										label9.text = model1.filterName(lotteryFiles[7])
									}
									Thread.sleep(model1.time_select)
									model1.play_wav_drum_winner.play()
									ViewUtil.invokeLater {
										label8.icon = swing.imageIcon(file:lotteryFiles[6], width:width,height:height)
										label8.text = model1.filterName(lotteryFiles[6])
									}
									Thread.sleep(model1.time_select)
									model1.play_wav_drum_winner.play()
									ViewUtil.invokeLater {
										label7.icon = swing.imageIcon(file:lotteryFiles[5], width:width,height:height)
										label7.text = model1.filterName(lotteryFiles[5])
									}
									Thread.sleep(model1.time_select)
									model1.play_wav_drum_winner.play()
									ViewUtil.invokeLater {
										label4.icon = swing.imageIcon(file:lotteryFiles[3], width:width,height:height)
										label4.text = model1.filterName(lotteryFiles[3])
									}
									button2.setEnabled(true)
									model1.play_wav_drum_start.stop()
								}
							})
							button2 = button(label:'抽獎',enabled:false,actionPerformed:{
								// 按抽獎
								int winner = model1.randomIndex(8)
								File winnerFile = lotteryFiles[winner]

								def winnerLabel

								switch(winner){
									case 0:
										winnerLabel = label1
										break
									case 1:
										winnerLabel = label2
										break
									case 2:
										winnerLabel = label3
										break
									case 3:
										winnerLabel = label4
										break
									case 4:
										winnerLabel = label6
										break
									case 5:
										winnerLabel = label7
										break
									case 6:
										winnerLabel = label8
										break
									case 7:
										winnerLabel = label9
										break
								}
								model1.winner(winnerFile)

								if(!labels)
									labels = [
										label1,
										label2,
										label3,
										label6,
										label9,
										label8,
										label7,
										label4
									]
								Thread.start {
									ViewUtil.invokeLater {
										JButton b1 = button1
										JButton b2 = button2
										JButton b3 = button3
										label5.border = blackBorder
										b1.setEnabled(false)
										b2.setEnabled(false)
										b3.setEnabled(false)
									}
									//first loop
									model1.play_wav_start.play()
									for(int index in 0..7){
										ViewUtil.invokeLater {
											labels.each { it.border = blackBorder }
											labels[index].border = redBorder
										}
										Thread.sleep(model1.time_lottery_start)
									}
									model1.play_wav_start.stop()
									//roll loop
									for(int i in 1..model1.lottery_roll_loop){
										model1.play_wav_roll.play()
										for(int index in 0..7){
											ViewUtil.invokeLater {
												labels.each { it.border = blackBorder }
												labels[index].border = redBorder
											}
											Thread.sleep(model1.time_lottery_roll)
										}
										model1.play_wav_roll.stop()
									}
									//end loop
									model1.play_wav_start.play()
									for(int index in 0..7){
										ViewUtil.invokeLater {
											labels.each { it.border = blackBorder }
											labels[index].border = redBorder
										}
										//winner 中獎者，閃3下
										if(labels[index] == winnerLabel){
											model1.play_wav_start.stop()
											Thread.sleep(model1.time_lottery_winner)
											model1.play_wav_winner.play()
											for(int i in 0..2){
												ViewUtil.invokeLater {
													labels[index].border = redBorder
												}
												Thread.sleep(model1.time_lottery_winner)
												ViewUtil.invokeLater {
													labels[index].border = blackBorder
												}
												Thread.sleep(model1.time_lottery_winner)
											}
											ViewUtil.invokeLater {
												labels[index].border = redBorder
												//中獎大圖
												if(!winnerWindow){
													swing.edt {
														winnerWindow = window(owner:rootFrame, visible:true, pack:true){
															vbox{
																panel{
																	winnerWindowLabel = label(
																			icon:swing.imageIcon(file:winnerFile, width:width*2,height:height*2),
																			text:labels[index].text,
																			horizontalTextPosition:JLabel.CENTER,
																			verticalTextPosition:JLabel.BOTTOM,
																			border:redBorder)
																}
																panel{
																	button(label:'中獎！',actionPerformed:{ winnerWindow.visible = false })
																}
															}
														}
													}
													ViewUtil.centerWindow(winnerWindow)
												}else{
													winnerWindowLabel.icon = swing.imageIcon(file:winnerFile, width:width*2,height:height*2)
													winnerWindow.visible = true
												}
											}
											break
										}
										Thread.sleep(model1.time_lottery_end)
									}
									ViewUtil.invokeLater {
										JButton b1 = button1
										JButton b3 = button3
										b1.setEnabled(true)
										b3.setEnabled(true)
									}
								}
							})
							button3 = button(label:'記錄',actionPerformed:{
								// 按記錄
								String winners = '中獎者如下：\n'
								model1.winnerFiles.each {
									String name = model1.fileName(it)
									winners += "${name}\n"
								}
								JOptionPane.showMessageDialog(rootFrame,winners)
							})
						}
					}
				}
			}
		}
		ViewUtil.centerWindow(rootFrame,true)
	}
}
